package com.base.webapp.respm;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class BaseResp {

	public static final int OK = 0;
	public static final int FAIL = 1;
	public static final int CHECK_SUM_ERROR = 2;

	public static Logger logger = Logger.getLogger(BaseResp.class);

	private int errcode;
	private String errmsg = "ok";
	private Object data;

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	@JsonIgnore
	public boolean isOK() {
		return errcode == OK;
	}

	public int getErrcode() {
		return errcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void ok() {
		this.errcode = OK;
		this.errmsg = "ok";
	}

	public void fail(int errcode, String errmsg) {
		this.errcode = errcode;
		this.errmsg = errmsg;
	}

	public boolean processResultValid(BindingResult result) {
		if (result.hasErrors()) {
			bindErrors(result.getFieldErrors());
			return false;
		}
		return true;
	}

	public BaseResp bindErrors(List<FieldError> errors) {
		if (errors != null) {
			boolean first = true;
			for (FieldError error : errors) {
				String message = error.getDefaultMessage();
				int index = message.indexOf(",");
				if (index > 0) {
					String code = message.substring(0, index);
					String msg = message.substring(index + 1);
					if (first) {
						try {
							errcode = Integer.parseInt(code);
						} catch (Exception e) {
							logger.error(
									"error code is not integer in ValidationMessages.properties [" + message + "]");
						}
						errmsg = msg;
						first = false;
					} else {
						errmsg += "," + msg;
					}
				} else {
					logger.error("error message format in ValidationMessages.properties [" + message + "]");
					errcode = -1;
					errmsg = message;
				}
			}
		}
		return this;
	}

	public BaseResp bindException(ResException e) {
		fail(e.getErrcode(), e.getErrmsg());
		return this;
	}

	public void userNotExist() {
		fail(10001, "用户不存在！");
	}

	public void userExist() {
		fail(10002, "用户已存在！");
	}

	public void phoneError() {
		fail(10003, "不正确的号码！");
	}

	public void userPasswordError() {
		fail(10004, "密码错误！");
	}

	public void inviterNotExist() {
		fail(10005, "推荐人不存在！");
	}

	public void captchaTooFrequent() {
		fail(10006, "短信请求过于频繁！");
	}

	public void captchaServerError() {
		fail(10007, "验证码服务器异常！");
	}

	public void captchaSendFail() {
		fail(10008, "验证码发送失败！");
	}

	public void captchaExpired() {
		fail(10009, "验证码已过期！");
	}

	public void captchaInvalid() {
		fail(10010, "无效验证码！");
	}

	public void loginExpired() {
		fail(10011, "登陆已过期");
	}

	public void paramError() {
		fail(10012, "参数格式错误！");
	}

	public void paramNull() {
		fail(10013, "参数为空！");
	}

	public void tokenInvalid() {
		fail(10014, "无效令牌！");
	}

	public void tokenExpired() {
		fail(10015, "令牌过期！");
	}

	public void productNotExist() {
		fail(10016, "商品不存在！");
	}

	public void bankNotExist() {
		fail(10017, "银行不存在！");
	}

	public void addressInvalid() {
		fail(10018, "地址不正确！");
	}

	public void profileInvalid() {
		fail(10019, "已生效资料不可修改！");
	}

	public void imageNotExists(IllegalStateException e) {
		fail(10020, "上传图片已经移走！" + e.getMessage());
		logger.warn("10020,上传图片已经移走！", e);
	}

	public void imageIOError(IOException e) {
		fail(10021, "图片转储读写失败！" + e.getMessage());
		logger.warn("10021,图片转储读写失败！", e);
	}

	public void appTypeNotExist() {
		fail(30016, "app类型不存在！");
	}

	public void imageSizeZero() {
		fail(10022, "上传图片大小为零！");
	}

	public void shopNotExist() {
		fail(10615, "店铺不存在！");
	}

	public void productIdEmpty() {
		fail(10700, "productId为空！");
	}

	public void shopProductNotExist() {
		fail(10800, "商品不存在！");
	}

	public void shopShopProductSelling() {
		fail(10900, "商品已上架！");
	}

	public void attributeNotExist() {
		fail(10975, "规格属性不存在");
	}

	public void favoriteNotExist() {
		fail(10900, "收藏不存在！");
	}

	public void evaluationNotExist() {
		fail(11000, "评价为空！");
	}

	public void addresseeNotExist() {
		fail(11100, "地址为空！");
	}

	public void itemNotExist() {
		fail(11310, "item为空！");
	}

	public void branchShopsNotExist() {
		fail(11320, "分店为空！");
	}

}
